﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.Collections;

namespace ArkEC.CoreCom.Datastruct
{
    /// <summary>
    /// 对象转换类
    /// </summary>
    public class ObjectConvertion
    {
        /// <summary>
        /// Json转化（支持string、json、业务对象）,支持范型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(string jsonStr)
        {
            T obj = JsonConvert.DeserializeObject<T>(jsonStr);
        
            return obj;
        }

        /// <summary>
        /// Json转化（支持string、json、业务对象）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static object JsonToObject(Type objType, string jsonStr)
        {
            object obj = JsonConvert.DeserializeObject(jsonStr, objType);

            return obj;
        }

        /// <summary>
        /// 对象转换为Json
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToJson(object obj)
        {
            string json = "";

            if (obj != null)
            {
                string typeName = obj.GetType().Name;
                if (typeName == "String" || typeName == "Int")
                {
                    json = ValueToString(obj);
                }
                else
                {
                    json = JsonConvert.SerializeObject(obj);
                }
            }

            return json;
        }

        /// <summary>
        /// 从数据库中转换为整形
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int DbToInt(object obj)
        {
            return (System.DBNull.Value == obj) ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 从数据库中转换为字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string DbToString(object obj)
        {
            return (System.DBNull.Value == obj) ? string.Empty : Convert.ToString(obj);
        }

        /// <summary>
        /// 从数据库中转换为布尔型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool DbToBool(object obj)
        {
            return (System.DBNull.Value == obj) ? false : Convert.ToBoolean(obj);
        }

        /// <summary>
        /// 从数据库中转换为日期
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime? DbToDatatime(object obj)
        {
            DateTime? time = null;
            if (System.DBNull.Value != obj)
            {
                time = Convert.ToDateTime(obj);
            }
            return time;
        }

        /// <summary>
        /// 值类型转换为字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ValueToString(object obj)
        {
            return (null == obj || string.IsNullOrEmpty(obj.ToString())) ? string.Empty : obj.ToString();
        }

        /// <summary>
        /// 字符串转换为整形
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int StringToInt(string str)
        {
            int defaultValue = 0;
            if (!string.IsNullOrEmpty(str))
            {
                if (int.TryParse(str, out defaultValue))
                {
                    return defaultValue;
                }
            }
            return defaultValue;
        }

        /// <summary>
        /// 集合拼接为字符串
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public static string CollectionToString(ICollection items)
        {
            if (null != items && 0 < items.Count)
            {
                StringBuilder builder = new StringBuilder();
                foreach (object obj in items)
                {
                    builder.AppendFormat(";{0}", ObjectConvertion.ValueToString(obj));
                }
                if (0 < builder.Length)
                {
                    builder.Remove(0, 1);
                }
                return builder.ToString();
            }
            return string.Empty;
        }
    }
}
